﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class AdministerForum : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            if (Request.QueryString["Answer"] != null)
                this.MessageLabel.Text = Request.QueryString["Answer"].ToString();
            OnlineUser ou = (OnlineUser)Session["User"];
            string ForumId = null;
            if (Request.QueryString["ForumId"] != null)
                ForumId = Request.QueryString["ForumId"].ToString();
            else
                Response.Redirect("~/ForumHomePage.aspx");
            ForumIdHiddenField.Value = ForumId;
            if (GND.ReadNivdalBoolData("@ForumId", "Nivdal_ForumIsForumPublic", ForumId))
                Response.Redirect("~/ForumHomePage.aspx");
            if (!GND.ReadNivdalBoolData("@UserId", "@ForumId", "Nivdal_ForumCanUserAdministerForum", ou.UserId, ForumId))
                Response.Redirect("~/ForumHomePage.aspx");
            if (!IsPostBack)
            {
                string ForumName = GND.ReadNivdalStringData("@ForumId", "Nivdal_ForumGetForumName", ForumId);
                this.ForumNameTextBox.Text = ForumName;
                this.PublicInfoTextArea.Text = GND.ReadNivdalStringData("@ForumId", "Nivdal_ForumGetForumPublicInfo", ForumId);
                this.PrivateInfoTextArea.Text = GND.ReadNivdalStringData("@ForumId", "Nivdal_ForumGetForumPrivateInfo", ForumId);
                this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.AdministerPrivateForum_PageTitle + ": " + ForumName;
                this.MembershipApplicationsSqlDataSource.SelectParameters.Add("ForumId", ForumId);
                this.MembershipApplicationsSqlDataSource.SelectParameters.Add("Language", ou.Language);
                this.ExpelUserSqlDataSource.SelectParameters.Add("ForumId", ForumId);
                this.ExpelUserSqlDataSource.SelectParameters.Add("Language", ou.Language);
            }
            this.BackToTheForumHyperLink.NavigateUrl = "ForumDetails.aspx?Id=" + ForumId;
            this.ApplicationsPanel.Visible = (this.MembershipApplicationsGridView.Rows.Count > 0);
           

        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();

            String ForumName = this.ForumNameTextBox.Text.Trim();
            String PublicInfo = this.PublicInfoTextArea.Text.Trim();
            String PrivateInfo = this.PrivateInfoTextArea.Text.Trim();

            this.MessageLabel.Text = ForumName;

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_ForumUpdateForumDetails";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_ForumId = new SqlParameter();
            dbParam_ForumId.ParameterName = "@ForumId";
            dbParam_ForumId.Value = ForumIdHiddenField.Value;
            dbParam_ForumId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ForumId);

            SqlParameter dbParam_ForumName = new SqlParameter();
            dbParam_ForumName.ParameterName = "@ForumName";
            dbParam_ForumName.Value = ForumName;
            dbParam_ForumName.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ForumName);

            SqlParameter dbParam_PublicInfo = new SqlParameter();
            dbParam_PublicInfo.ParameterName = "@PublicInfo";
            dbParam_PublicInfo.Value = PublicInfo;
            dbParam_PublicInfo.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_PublicInfo);

            SqlParameter dbParam_PrivateInfo = new SqlParameter();
            dbParam_PrivateInfo.ParameterName = "@PrivateInfo";
            dbParam_PrivateInfo.Value = PrivateInfo;
            dbParam_PrivateInfo.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_PrivateInfo);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            SqlParameter dbParam_Language = new SqlParameter();
            dbParam_Language.ParameterName = "@Language";
            dbParam_Language.Value = GND.GetUserLanguage();
            dbParam_Language.DbType = DbType.Decimal;
            dbCommand.Parameters.Add(dbParam_Language);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                ////Response.Redirect("AdministerForum.aspx?ForumId=" + dbParam_ForumId.Value.ToString() + "&Answer=" + dbParam_Answer.Value.ToString());
            }
            catch (Exception SQLDBException)
            {
                this.MessageLabel.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
            }

        }

        protected void MembershipApplicationsGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Visible = false;
        }




        protected void ApproveApplication(string ApplicationId)
        {
            string ForumId = ForumIdHiddenField.Value;
  
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_ForumApproveApplication";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_ApplicationId = new SqlParameter();
            dbParam_ApplicationId.ParameterName = "@ApplicationId";
            dbParam_ApplicationId.Value = ApplicationId;
            dbParam_ApplicationId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ApplicationId);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            SqlParameter dbParam_Language = new SqlParameter();
            dbParam_Language.ParameterName = "@Language";
            dbParam_Language.Value = GND.GetUserLanguage();
            dbParam_Language.DbType = DbType.Decimal;
            dbCommand.Parameters.Add(dbParam_Language);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                Response.Redirect("AdministerForum.aspx?ForumId=" + ForumId + "&Answer=" + dbParam_Answer.Value.ToString());
            }
            catch (Exception SQLDBException)
            {
                this.MessageLabel.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
            }

        }


        protected void DeclineApplication(string ApplicationId)
        {
            string ForumId = ForumIdHiddenField.Value;

            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_ForumDeclineApplication";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_ApplicationId = new SqlParameter();
            dbParam_ApplicationId.ParameterName = "@ApplicationId";
            dbParam_ApplicationId.Value = ApplicationId;
            dbParam_ApplicationId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ApplicationId);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            SqlParameter dbParam_Language = new SqlParameter();
            dbParam_Language.ParameterName = "@Language";
            dbParam_Language.Value = GND.GetUserLanguage();
            dbParam_Language.DbType = DbType.Decimal;
            dbCommand.Parameters.Add(dbParam_Language);

            try
            {
                dbConnection.Open();
                dbCommand.ExecuteScalar();
                Response.Redirect("AdministerForum.aspx?ForumId=" + ForumId + "&Answer=" + dbParam_Answer.Value.ToString());
            }
            catch (Exception SQLDBException)
            {
                this.MessageLabel.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
            }

        }


        protected void AdminMemberShip(Object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "Approve")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = MembershipApplicationsGridView.Rows[index];
                string ApplicationId = row.Cells[0].Text;
                //this.Message.Text = row.Cells[0].Text.ToString();
                //this.MessageLabel.Text = ApplicationId;
                ApproveApplication(ApplicationId);
            }
            if (e.CommandName == "Decline")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = MembershipApplicationsGridView.Rows[index];
                string ApplicationId = row.Cells[0].Text;
                //this.Message.Text = row.Cells[0].Text.ToString();
                //this.MessageLabel.Text = ApplicationId;
                DeclineApplication(ApplicationId);
            }
        }

        protected void ExpelUserGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Visible = false;
        }

        protected void ExpelUser(string UserId)
        {
            string ForumId = null;
            if (Request.QueryString["ForumId"] != null)
                ForumId = Request.QueryString["ForumId"].ToString();
            else
                Response.Redirect("~/ForumHomePage.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_ForumUserLeaveForum";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_UserId = new SqlParameter();
            dbParam_UserId.ParameterName = "@UserId";
            dbParam_UserId.Value = UserId;
            dbParam_UserId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_UserId);

            SqlParameter dbParam_ForumId = new SqlParameter();
            dbParam_ForumId.ParameterName = "@ForumId";
            dbParam_ForumId.Value = ForumId;
            dbParam_ForumId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ForumId);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                Response.Redirect("AdministerForum.aspx?ForumId=" + ForumId + "&Answer=" + dbParam_Answer.Value.ToString());
            }
            catch (Exception SQLDBException)
            {
                this.MessageLabel.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
                
            }

        }



        protected void AdminExpelUser(Object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "Expel")
            {
                // get the row index stored in the CommandArgument property
                //int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                ///GridViewRow row = this.ExpelUserGridView.Rows[index];
                //GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
                ///string UserId = row.Cells[0].Text;
                //string UserId = selectedRow.Cells[0].Text;
                //this.Message.Text = row.Cells[0].Text.ToString();
                //this.MessageLabel.Text = UserId;
                ExpelUser(e.CommandArgument.ToString());
            }

        }

        protected string ApproveLinkButtonOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.AdministerForum_ApproveApplicationLinkButton + "')");
        }

        protected string DeclineApplicationButtonOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.AdministerForum_DeclineApplicationLinkButton + "')");
        }

        protected string ExpelLinkButtonOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.AdministerForum_ExpelLinkButton + "')");
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }


    }
}